﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TodoApp.EntityFrameworkCore;
using TodoApp.ResultModel;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories.Dapper;
using Volo.Abp.EntityFrameworkCore;

namespace TodoApp.TMS_maintain.TMS_accident
{
    public class TMS_accidents : DapperRepository<TodoAppDbContext>, ITransientDependency, ITMS_accidents
    {
        public TMS_accidents(IDbContextProvider<TodoAppDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        #region 油费显示
        public async Task<PageResponse<TMS_accidentDto>> GetAcc(string Acctitel, string AccNumber, int pageindex = 1, int pagesize = 5)
        {
            var result = new PageResponse<TMS_accidentDto>();
            var dbConnection = await GetDbConnectionAsync();
            var sql = "select * from TMS_accidentModel where 1=1 ";
            if (!string.IsNullOrEmpty(Acctitel))
            {
                sql += $" and Acctitel like '%{Acctitel}%'";
            }
            if (!string.IsNullOrEmpty(AccNumber))
            {
                sql += $" and AccNumber  like '%{AccNumber}%'";
            }

            var list = await dbConnection.QueryAsync<TMS_accidentDto>(sql, transaction: await GetDbTransactionAsync());
            result.PageData = list.ToList();
            result.PageTotal = list.Count();
            sql += $" order by Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only ";
            var quer = await dbConnection.QueryAsync<TMS_accidentDto>(sql, transaction: await GetDbTransactionAsync());
            result.PageData = quer.ToList();
            return result;

        }
        #endregion

        #region 油费添加
        public async Task<int> AccAdds(TMS_accidentDto info)
        {

            var dbConnection = await GetDbConnectionAsync();

            info.Id = Guid.NewGuid();

            var list = await dbConnection.ExecuteAsync($"insert into TMS_accidentModel values ('{info.Id}','{info.Acctitel}','{info.AccNumber}','{info.AccPeop}','{info.AccTim}','{info.Accbao}','{info.Accyuan}','{info.Accmiao}','{info.Accjing}','{info.AccTxt}')", null,
                 await GetDbTransactionAsync());
            if (list > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        #endregion

        #region 油费删除
        public async Task<int> DeletAcc(string Id)
        {

            var dbConnection = await GetDbConnectionAsync();
            return await dbConnection.ExecuteAsync($"delete from TMS_accidentModel where Id='{Id}'", null,
                 await GetDbTransactionAsync());

        }

        #endregion

        #region 油费修改
        public async Task<int> UpdateAccup(TMS_accidentDto tMS_Induction)
        {
            var dbConnection = await GetDbConnectionAsync();
            return await dbConnection.ExecuteAsync($"update TMS_accidentModel set Acctitel='{tMS_Induction.Acctitel}',AccNumber='{tMS_Induction.AccNumber}',AccPeop='{tMS_Induction.AccPeop}',AccTim='{tMS_Induction.AccTim}',Accbao='{tMS_Induction.Accbao}',Accyuan='{tMS_Induction.Accyuan}',Accmiao='{tMS_Induction.Accmiao}',Accjing='{tMS_Induction.Accjing}',AccTxt='{tMS_Induction.AccTxt}' where Id='{tMS_Induction.Id}'", null, await GetDbTransactionAsync());
        }
        #endregion
    }
}
